package com.stadium.web.service.impl;

import com.fastsite.cache.ehcache.utils.EhCacheUtil;
import com.fastsite.common.api.ApiTokenObject;
import com.fastsite.common.api.ApiTokenObjectImpl;
import com.fastsite.common.enums.StatusEnum;
import com.fastsite.dao.utils.PageSort;
import com.fastsite.utils.EncryptUtil;
import com.stadium.common.StadiumConstant;
import com.stadium.web.domain.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.stadium.web.repository.UserRepository;
import com.stadium.web.domain.User;
import com.stadium.web.service.UserService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-14
 */
@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserRepository userRepository;

    /**
     * 根据用户ID查询用户数据
     * @param id 用户ID
     */
    @Override
    @Transactional
    public User getById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<User> getPageList(Example<User> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest(null,Sort.Direction.ASC);
        return userRepository.findAll(example, page);
    }


    /**
     * 保存用户
     * @param user 用户 实体类
     */
    @Override
    public User save(User user){
        return userRepository.save(user);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> ids){
        // 删除用户时取消与用户和菜单的关联
        return userRepository.updateStatusByIdIn(statusEnum.getCode(), ids) > 0;
    }

    @Override
    public User findByPhoneAndPassword(String username, String password) {
        return userRepository.findByPhoneAndPassword(username,password);
    }

    @Override
    public TokenUser getTokenUserByToken(String token) {
        TokenUser tokenUser = (TokenUser) EhCacheUtil.getValue(StadiumConstant.TOKEN_CACHE,token);
        return tokenUser;
    }

    @Override
    public String createToken(User user) {
        //用户名家密码 加时间戳
        final String TOKEN =  EncryptUtil.encrypt(user.getName()+user.getPassword()+LocalDateTime.now().toEpochSecond(ZoneOffset.MAX), StadiumConstant.SALT);
        TokenUser tokenUser = new TokenUser();
        String[] scopes ={};
        //密码空置
        user.setPassword(null);
        tokenUser.setId(user.getId());
        tokenUser.setUser(user);
        tokenUser.setUserName(user.getName());
        ApiTokenObject apiTokenObject = new ApiTokenObjectImpl(tokenUser,TOKEN,scopes);
        EhCacheUtil.setValue(StadiumConstant.TOKEN_CACHE,TOKEN,apiTokenObject);
        return TOKEN;
    }

    @Override
    public User findByUsername(String phone) {
        return userRepository.findByPhone(phone);
    }

}
